﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Shell; 
using System.Xml;
using System.Xml.XPath;
using Microsoft.Phone.Controls;
using HtmlAgilityPack;
using Coding4Fun.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using sodexo;
namespace sodexo
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        private sodexoWebClient sWclient;
        static bool initialized;
        static bool isConnected;
        public MainPage()
        {
            InitializeComponent();
            initialized = false;
            isConnected = false;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            isConnected = Microsoft.Phone.Net.NetworkInformation.DeviceNetworkInformation.IsNetworkAvailable;
            
            
            if (!initialized)
            {
                initialized = true;
                if (!isConnected)
                {
                    MessageBox.Show("网络异常，请稍后再试!");
                    return;
                }
                sWclient = new sodexoWebClient("http://www.club.cn.sodexo.com/");
                sWclient.HomePageLoadedEvent += InitialCardType;
                sWclient.init();
                
            }
        }
        private void InitialCardType(object sender, HtmlDocumentLoadCompleted args)
        {
            int cardTypeCount = sWclient.CardType.Count;
            bool initialSuccess = true;
            ToastPrompt message = new ToastPrompt();
            if (args.Error != null)
            {
                message.Title = "错误!";
                message.Message = args.Error.Message;
                message.Show();
            }
            if (cardTypeCount > 0)
            {
                this.listPickerCardType.ItemsSource = sWclient.CardType;
            }
            else
            {
                //MessageBox.Show("无法获取卡片类型数据信息:" + args.Error.Message);
                
                initialSuccess = false;
            }

            int districtCount = sWclient.Districts.Count;
            if (districtCount > 0)
            {
                this.listPickerDistrict.ItemsSource = sWclient.Districts;
            }
            else
            {
               // MessageBox.Show("无法获取地区数据信息:" + args.Error.Message);
                initialSuccess = false;
            }
            (ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = initialSuccess;
            (ApplicationBar.MenuItems[0] as ApplicationBarMenuItem).IsEnabled = initialSuccess;
           // (ApplicationBar.MenuItems[1] as ApplicationBarMenuItem).IsEnabled = initialSuccess;
            this.performanceProgressBarUpdate.IsIndeterminate = !initialSuccess;
            if(!initialSuccess)
            {
                sWclient.init();
            }
            
        }

        private void listPickerCardType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (initialized)
            {
                ListPicker listPicker = (ListPicker)sender;
                int i = listPicker.SelectedIndex;
                this.sWclient.cardTypeIndex = this.listPickerCardType.SelectedIndex;
            }
        }
        private void Search_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageResult.xaml", UriKind.Relative));
        }
        private void buttonSearch_Click(object sender, RoutedEventArgs e)
        {
            Search_Click(sender, e);
        }


        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            var targetPage = e.Content as PageResult;
            if (targetPage != null)
            {
                targetPage.SWclient = this.sWclient;
            }
            

        }

        private void listPickerDistrict_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (initialized)
            {
                //this.sWclient.districtIndex = this.listPickerDistrict.SelectedIndex;
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            Search_Click(sender, e);
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            Search_Click(sender, e);
        }

        private void ApplicationBarMenuItem_About(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageAbout.xaml", UriKind.Relative));
            
        }

    }
}